package com.jn.erp.wechat.staff.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jn.erp.common.core.page.TableDataInfo;
import com.jn.erp.common.core.result.AjaxResult;
import com.jn.erp.web.api.common.entity.TLabel;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.service.TCourseOrderService;
import com.jn.erp.web.api.interaction.entity.TEvaluate;
import com.jn.erp.web.api.interaction.entity.TEvaluateTemplate;
import com.jn.erp.web.api.interaction.entity.dto.TEvaluateTemplateDto;
import com.jn.erp.web.api.interaction.entity.vo.TEvaluateVo;
import com.jn.erp.web.api.interaction.service.TEvaluateService;
import com.jn.erp.web.api.interaction.service.TEvaluateTemplateService;
import com.jn.erp.wechat.common.api.controller.base.BaseWxAuthController;
import com.jn.erp.wechat.common.api.entity.dto.WxUserDto;
import com.jn.erp.wechat.core.WxTableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 *  评价
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/12/5 11:20
 */
@RestController
@RequestMapping("/wx/evaluate")
public class MpEvaluateController  extends BaseWxAuthController {

    @Autowired
    private TEvaluateService tEvaluateService;

    @Autowired
    private TEvaluateTemplateService tEvaluateTemplateService;

    @Autowired
    private TCourseOrderService tCourseOrderService;
    /**
     评论列表  /tEvaluate/evaluateList
     */
    @GetMapping("/evaluateList")
    public WxTableDataInfo evaluateList(TEvaluateVo tEvaluateVo){
        WxUserDto wxUser = getWxUser();
        startPage();
        //机构id
        tEvaluateVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(wxUser.getUserId()));
        List<TEvaluate> collect = tEvaluateService.evaluateUnifyList(tEvaluateVo);

//        List<TEvaluate> list = tEvaluateService.list(new QueryWrapper<TEvaluate>().eq("order_no", tEvaluateVo.getOrderNo()).eq("is_delete", "0").eq("state", tEvaluateVo.getState()).eq("type",1));
//        List<TEvaluate> collect = list.stream().distinct().collect(Collectors.toList());
//        List<TCourseOrder> list1 = tCourseOrderService.list(new QueryWrapper<TCourseOrder>().eq("order_no", tEvaluateVo.getOrderNo()).eq("is_delete", "0").eq("org_id", wxUser.getOrgIdLong()));
//        for (TCourseOrder courseOrder : list1) {
//            for (TEvaluate tEvaluate : collect) {
//                if (courseOrder.getTeacher() == getUserId()) {
//                    tEvaluate.setCode("0");
//                } else {
//                    tEvaluate.setCode("1");
//                }
//            }
//        }
        return getWxDataTable(collect);
    }


    /**
     评论发布     /tEvaluate/updateEvaluate
     */
    @PostMapping("/updateEvaluate")
    public AjaxResult updateEvaluate(@RequestBody TEvaluateVo tEvaluateVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tEvaluateVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tEvaluateVo.setUpdateUser(String.valueOf(wxUser.getUserId()) );
        tEvaluateVo.setUpdateDate(LocalDateTime.now() );
        String code = tEvaluateService.updateEvaluate(tEvaluateVo);
        return AjaxResult.success(code);
    }


    /**
     评论详情  /tEvaluate/evaluateGetById
     */
    @PostMapping("/evaluateGetById")
    public AjaxResult evaluateGetById(@RequestBody TEvaluateVo tEvaluateVo){
        WxUserDto wxUser = getWxUser();
        //机构id
        tEvaluateVo.setOrgId(wxUser.getOrgIdLong());
        //当前登录人id
        tEvaluateVo.setCreateUser(String.valueOf(wxUser.getUserId()) );
        TEvaluate tEvaluate = tEvaluateService.evaluateGetById(tEvaluateVo);
        return AjaxResult.success(tEvaluate);
    }



    //------------------------------模版操作---------------------------------------

    /**
     * 模版列表   /tEvaluateTemplate/list
     */
    @GetMapping("/templateList")
    public WxTableDataInfo templateList(TEvaluateTemplate tEvaluateTemplate) {
        WxUserDto wxUser = getWxUser();
        tEvaluateTemplate.setOrgId(wxUser.getOrgIdLong());
        List<TEvaluateTemplateDto> list = tEvaluateTemplateService.templateList(tEvaluateTemplate);
        return getWxDataTable(list);
    }

    /**
     *  模版详情     /tEvaluateTemplate/getTempById
     * */
    @PostMapping("/getTempById")
    public AjaxResult getTempById(@RequestBody TEvaluateTemplate tEvaluateTemplate) {
        WxUserDto wxUser = getWxUser();
        tEvaluateTemplate.setOrgId(wxUser.getOrgIdLong());
        List<TEvaluateTemplateDto> list = tEvaluateTemplateService.templateList(tEvaluateTemplate);
        List<TLabel> tLabels1 = new ArrayList<>();
        for (TEvaluateTemplateDto tEvaluateTemplateDto : list) {
            // 标签名称
            String labelName1 = tEvaluateTemplateDto.getLabelName();
            String[] splitName = labelName1.split(",");
            // 标签id
            String labelId = tEvaluateTemplateDto.getLabelId();
            String[] splitId = labelId.split(",");
            // 标签排序
            String[] splitNum = tEvaluateTemplateDto.getOrderNum().split(",");

            int len = splitNum.length;
            for (int i = 0; i < len; i++) {
                TLabel tLabel = new TLabel();
                tLabel.setOrderNum(Integer.valueOf(splitNum[i]));
                tLabel.setId(Long.valueOf(splitId[i]));
                tLabel.setName(splitName[i]);
                tLabels1.add(tLabel);
            }
            tEvaluateTemplateDto.settName(tLabels1);
        }
        return AjaxResult.success(list);
    }




}
